package cn.cuilan.test.it.admin;

import cn.cuilan.entity.PositionArea;
import cn.cuilan.entity.PositionCity;
import cn.cuilan.entity.PositionCountry;
import cn.cuilan.entity.PositionProvince;
import cn.cuilan.repository.PositionAreaRepository;
import cn.cuilan.repository.PositionCityRepository;
import cn.cuilan.repository.PositionCountryRepository;
import cn.cuilan.repository.PositionProvinceRepository;
import cn.cuilan.test.common.CommonTest;
import cn.cuilan.utils.Results;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.Assert.assertTrue;

/**
 * PositionController集成测试
 *
 * @author zhang.yue
 */
public class AdminPositionControllerIT extends CommonTest {

    /**
     * 接口URL前缀
     */
    private final static String URL_PREFX = "/admin/position";

    @Autowired
    private PositionCountryRepository positionCountryRepository;

    @Autowired
    private PositionProvinceRepository positionProvinceRepository;

    @Autowired
    private PositionAreaRepository positionAreaRepository;

    @Autowired
    private PositionCityRepository positionCityRepository;

    @Before
    public void before() {
        PositionCountry positionCountry = new PositionCountry();
        positionCountry.setCountryCode(86);
        positionCountry.setCountryName("中华人民共和国");
        positionCountry.setLanguageCode("zh");
        positionCountryRepository.save(positionCountry);

        PositionProvince positionProvince = new PositionProvince();
        positionProvince.setProvinceCode(130000);
        positionProvince.setProvinceName("河北省");
        positionProvince.setCountryId(86);
        positionProvinceRepository.save(positionProvince);

        positionProvinceRepository.save(positionProvince);

        PositionArea positionArea = new PositionArea();
        positionArea.setAreaCode(130100);
        positionArea.setAreaName("石家庄市");
        positionArea.setProvinceId(130000);
        positionAreaRepository.save(positionArea);

        PositionCity positionCity = new PositionCity();
        positionCity.setCityCode(130102);
        positionCity.setCityName("长安区");
        positionCity.setAreaId(130100);
        positionCityRepository.save(positionCity);
    }

    @Test
    public void countryIT() {
        assertTrue(isSuccess(testRestTemplate.getForObject(URL_PREFX + "/country", Results.class)));
    }

    @Test
    public void provinceIT() {
        int cid = 86;
        assertTrue(isSuccess(testRestTemplate.getForObject(URL_PREFX + "/province?cid=" + cid, Results.class)));
    }

    @Test
    public void areaIT() {
        int pid = 130000;
        assertTrue(isSuccess(testRestTemplate.getForObject(URL_PREFX + "/area?pid=" + pid, Results.class)));
    }

    @Test
    public void cityIT() {
        int aid = 130100;
        assertTrue(isSuccess(testRestTemplate.getForObject(URL_PREFX + "/city?aid=" + aid, Results.class)));
    }

}